﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration.Provider;
using System.Configuration;
using System.Collections.Specialized;
using System.Collections;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using XWord.Data.Provider;

namespace XWord.Data
{
    [Serializable]
    public abstract class DbProvider : ProviderBase
    {
        
        private static DbProvider provider;
        public Database db;
        private static GenericProviderCollection<DbProvider> providers = ProviderConfigurationHelper.LoadProviderCollection<DbProvider>("Database", out provider);

        /// <summary>
        /// Returns the currently configured DbProvider.
        /// </summary>
        /// <returns></returns>
        public static DbProvider Instance
        {
            get { return provider; }
        }


        public override void Initialize(string name, NameValueCollection configValue)
        {
            base.Initialize(name, configValue);
        }

        #region Sqlhelp

        public int ExecuteNonQuery(string query)
        {
            return SqlHelper.ExecuteNonQuery(query, db);
        }

        public int ExecuteNonQuery(string query, DbParameter[] parameters)
        {
            return SqlHelper.ExecuteNonQuery(query, db, parameters);
        }

        public IDataReader ExecuteReader(string query)
        {
            return SqlHelper.ExecuteReader(query, db);
        }

        protected IDataReader ExecuteReader(string query, DbParameter[] parameters)
        {
            return SqlHelper.ExecuteReader(query, db, parameters);
        }

        protected int ExecuteScalar(string query)
        {
            return Convert.ToInt32(SqlHelper.ExecuteScalar(query, db));
        }

        protected object ExecuteScalar(string query, DbParameter[] parameters)
        {
            return SqlHelper.ExecuteScalar(query, db, parameters);
        }

        #endregion

        private string _connectionString;
        /// <summary>
        /// Gets or sets the connection string.
        /// </summary>
        /// <value></value>
        public virtual string ConnectionString
        {
            //TODO: Make this protected.
            get { return _connectionString; }
            set { _connectionString = value; }
        }
    }
}
